﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_pages : System.Web.UI.Page
{
    static string id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"].ToString().Length == 0)
                Response.Redirect("login.aspx");

            id = Request.QueryString["func"].ToString();
            clsMenu clsMN = new clsMenu();
            clsMN.exportDataToDropDownList(ref cmbMenu);
            nhapDanhSach();
        }
    }
    protected void nhapDanhSach()
    {
        clsTableAction clsTable = new clsTableAction("Page");
        SqlDataReader sqlDare = clsTable.exportDataReader("*", " id_p=" + id);
        if (sqlDare.Read())
        {
            txtTitle.Text = sqlDare[2].ToString();
            EditorSumary.Text = sqlDare[3].ToString().Replace("`", "'");
            EditorContent.Text = sqlDare[4].ToString().Replace("`", "'");
        }
        clsTable.free();
    }    
    protected void butOK_Click(object sender, EventArgs e)
    {
        //Upload hinh
        clsTableAction clsTable = new clsTableAction("Page");
        if (uploadImage(FileUploadImage, id + ".jpg"))
        {
            //Tien hanh luu
            string sql = string.Format("update page set titleVi=N'{0}', sumaryVi='{1}', contentVi=N'{2}', id_mn='{3}', image='{4}' where id_p='{5}'", txtTitle.Text, EditorSumary.Text.Replace("'", "`"), EditorContent.Text.Replace("'", "`"), cmbMenu.SelectedValue, id + ".jpg",  id);
            clsTable.runSQL(sql);
        }
        clsTable.free();
    }
    private Boolean uploadImage(FileUpload FileImage, string fileName)
    {
        try
        {
            if (FileImage.HasFile)
            {
                string path = Server.MapPath("../") + "/Page/" + fileName;
                FileImage.SaveAs(path);
                clsImages clsImg = new clsImages();
                clsImg.saveAndResizeImage(path, path, 115, 79, ".jpg");
                return true;
            }
            return false;
        }
        catch
        {
            return false;
        }
    }

}
